home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / progwin.zip / WINDOW2.ZIP / DIGCLOCK.C < prev    next >
Text File  |  1991-10-01  |  5KB  |  161 lines

  1.   /*-------------------------------------
  2.     BEEPER1.C -- Timer Demo Program No. 1
  3.          (c) Charles Petzold, 1990
  4.   ---------------------------------------*/
  5.   #include <windows.h>
  6.   #define ID_TIMER 1
  7.   #include <time.h>
  8.  
  9.   #define YEAR (datetime->tm_year % 100)
  10.   #define MONTH (datetime->tm_mon + 1)
  11.   #define MDAY (datetime->tm_mday)
  12.   #define WDAY (datetime->tm_wday)
  13.   #define HOUR (datetime->tm_hour)
  14.   #define MIN  (datetime->tm_min)
  15.   #define SEC  (datetime->tm_sec)
  16.   long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  17.   void SizeTheWindow (short *,short *,short *,short *);
  18.   char sDate [2],sTime [2], sAMPM [2][5] ;
  19.   int iDate, iTime ;
  20.  
  21.   int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  22.               LPSTR lpszCmdLine, int nCmdShow)
  23.        {
  24.        static char szAppName[] = "DigClock";
  25.        HWND        hwnd;
  26.        MSG       msg;
  27.        short       xStart, yStart, xClient, yClient;
  28.        WNDCLASS       wndclass;
  29.  
  30.        if (!hPrevInstance)
  31.         {
  32.         wndclass.style     = CS_HREDRAW | CS_VREDRAW;
  33.         wndclass.lpfnWndProc = WndProc;
  34.         wndclass.cbClsExtra     = 0;
  35.         wndclass.cbWndExtra     = 0;
  36.         wndclass.hInstance     = hInstance;
  37.         wndclass.hIcon     = NULL;
  38.         wndclass.hCursor     = LoadCursor (NULL, IDC_ARROW);
  39.         wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  40.         wndclass.lpszMenuName  = NULL;
  41.         wndclass.lpszClassName = szAppName;
  42.  
  43.         RegisterClass (&wndclass);
  44.         }
  45.   SizeTheWindow ( &xStart , &yStart, &xClient,&yClient);
  46.  
  47.        hwnd = CreateWindow (szAppName, szAppName,
  48.                 WS_POPUP | WS_DLGFRAME | WS_SYSMENU,
  49.                 xStart,yStart,
  50.                 xClient,yClient,
  51.                 NULL, NULL, hInstance, NULL);
  52.        if (!SetTimer (hwnd, ID_TIMER, 1000, NULL))
  53.          MessageBox (hwnd,"Too many clocks or timers!", szAppName,
  54.                   MB_ICONEXCLAMATION | MB_OK);
  55.          return FALSE;
  56.        ShowWindow (hwnd, SW_SHOWNOACTIVATE);
  57.        UpdateWindow (hwnd);
  58.  
  59.        while (GetMessage (&msg, NULL, 0, 0))
  60.         {
  61.         TranslateMessage (&msg);
  62.         DispatchMessage (&msg);
  63.         }
  64.        return msg.wParam;
  65.        }
  66.   void SizeTheWindow (short *pxStart, short *pyStart,
  67.               short *pxClient, short *pyClient)
  68.   {
  69.   HDC  hdc ;
  70.   TEXTMETRIC tm ;
  71.  
  72.   hdc = CreateIC("DISPLAY",NULL,NULL,NULL) ;
  73.   GetTextMetrics (hdc,&tm) ;
  74.   DeleteDC (hdc) ;
  75.   *pxClient = 2 * GetSystemMetrics (SM_CXDLGFRAME) + 16 * tm.tmAveCharWidth ;
  76.   *pxStart = GetSystemMetrics (SM_CXSCREEN) - *pxClient ;
  77.   *pyClient = 2 * GetSystemMetrics (SM_CYDLGFRAME) + 2 * tm.tmHeight ;
  78.   *pyStart = GetSystemMetrics (SM_CYSCREEN) - *pyClient ;
  79.  
  80.   }
  81.   void SetInternational (void)
  82.   {
  83.   static char cName [] = "intl";
  84.   iDate = GetProfileInt (cName, "iDate",0) ;
  85.   iTime = GetProfileInt (cName, "iTime",0) ;
  86.  
  87.   GetProfileString(cName, "sDate", "/", sDate, 2) ;
  88.   GetProfileString(cName, "sTime", ":", sTime, 2);
  89.   GetProfileString(cName,"s1159", "AM" ,sAMPM [0], 5);
  90.   GetProfileString(cName,"s1159", "PM" ,sAMPM [1], 5);
  91.  
  92.   }
  93.  
  94.   void WndPaint (HWND hwnd, HDC hdc)
  95.   {
  96.   static char szWday[] = "Sun\OMon\OThu\OFri\OSat";
  97.   char cBuffer[40];
  98.   long lTime;
  99.   RECT rect;
  100.   short nLength;
  101.   struct tm *datetime ;
  102.  
  103.   time (&lTime) ;
  104.   datetime = localtime (&lTime) ;
  105.   nLength = wsprintf(cBuffer, " %s  %d%s%02d%s%02d   \r\n",
  106.     (LPSTR) szWday + 4 * WDAY,
  107.     iDate == 1 ? MDAY : iDate == 2 ? YEAR : MONTH, (LPSTR) sDate,
  108.     iDate == 1 ? MONTH : iDate == 2 ? MONTH : MDAY , (LPSTR) sDate,
  109.     iDate == 1 ? YEAR : iDate == 2 ? MDAY : YEAR );
  110.  
  111. if (iTime == 1)
  112.     nLength += wsprintf (cBuffer + nLength, " %02d%s%02d%s%02d ",
  113.                 HOUR, (LPSTR) sTime, MIN, (LPSTR) sTime, SEC);
  114. else
  115.  
  116.     nLength += wsprintf (cBuffer + nLength, " %d%s%02d%s%02d %s ",
  117.            (HOUR % 12) ? (HOUR % 12) : 12,
  118.            (LPSTR) sTime, MIN, (LPSTR) sTime, SEC,
  119.            (LPSTR) sAMPM [HOUR / 12]) ;
  120. GetClientRect (hwnd, &rect) ;
  121. DrawText (hdc ,cBuffer, -1,&rect, DT_CENTER | DT_NOCLIP) ;
  122.  
  123. }
  124.  
  125.  
  126.   long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  127.  
  128.        {
  129.        HDC hdc ;
  130.        PAINTSTRUCT ps ;
  131.  
  132.        switch (message)
  133.         {
  134.         case WM_CREATE :
  135.          SetInternational ();
  136.          return 0 ;
  137.  
  138.         case WM_TIMER :
  139.          InvalidateRect (hwnd, NULL, FALSE) ;
  140.          return 0;
  141.         case WM_PAINT :
  142.          hdc = BeginPaint (hwnd,&ps) ;
  143.          WndPaint (hwnd,hdc) ;
  144.          EndPaint (hwnd, &ps) ;
  145.          return 0;
  146.  
  147.         case WM_WININICHANGE :
  148.  
  149.          SetInternational ();
  150.  
  151.          InvalidateRect (hwnd, NULL, FALSE) ;
  152.                  return 0 ;
  153.  
  154.         case WM_DESTROY:
  155.         KillTimer(hwnd,ID_TIMER);
  156.         PostQuitMessage(0);
  157.         return 0;
  158.          }
  159.        return DefWindowProc(hwnd,message,wParam,lParam);
  160.        }
  161.